package com.zw.weiyi.avtivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zw.weiyi.amd.InOutImageButton;
import com.zw.weiyi.amd.InOutRelativeLayout;
import com.zw.weiyi.anim.AbstractInOutAnimationSet.Direction;
import com.zw.weiyi.anim.AnimationControlUtils;
import com.zw.weiyi.anim.ClickAnimationSet;
import com.zw.weiyi.anim.NotClickAnimationSet;
import com.zw.weiyi.anim.ResetAnimationSet;
import com.zw.weiyi.phonesms.BaseActivity;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.PhoneUtil;

public class PhoneActivity extends BaseActivity {
	private boolean mAreButtonsShowins ;
	private InOutRelativeLayout mButtonsWrapper;
	private Animation mRotateInAnimation;
	private Animation mRotateOutAniamtion;
	private View mButtonControlIcon;
	private View mControlButton;
	private EditText ab;
	private ImageView fins;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phone_view);
        ab = (EditText) findViewById(R.id.ab);
        //关闭输入法，有焦点
        ab.setOnTouchListener(new OnTouchListener() {  
            public boolean onTouch(View v, MotionEvent event) { 
                ab.setInputType(InputType.TYPE_NULL); // 关闭软键盘      
                return false; 
            }
        });
        // 红色按钮
        mControlButton = findViewById(R.id.button_control_show_hide);
        fins = (ImageView) findViewById(R.id.fins);
        mControlButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				toggleButton();
			}
		}); 
        //关闭界面
        fins.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
        
        // 红色按钮中心，白色十字
        mButtonControlIcon = findViewById(R.id.button_control_show_hide_icon);
        mRotateInAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_story_add_button_in);
        mRotateOutAniamtion = AnimationUtils.loadAnimation(this, R.anim.rotate_story_add_button_out);
                
        // 所有弹出收回按钮视图集合
        mButtonsWrapper = (InOutRelativeLayout) findViewById(R.id.buttons_wrapper);
//        String bb = null;
        // 设置每个弹出收回按钮的点击事件，点击后放大并隐藏
        for (int i = 0, count = mButtonsWrapper.getChildCount(); i < count; i++) {
			if (mButtonsWrapper.getChildAt(i) instanceof InOutImageButton) {
				View view = mButtonsWrapper.getChildAt(i);
				final int a = i;
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View subject) {
						//打印具体那个按钮					
						po(a+1);						
					}
				});
			}
		}        
    }
    /**
     * 拨号操作：这里输入的1开始的12个
     * 1-10为数字位  10为0 ，11为拨号按钮  ，12为退格按钮
     */    
    String tt = "";
    public void po(int i){
    	if(i==10){
    		i=0;
    	}
    	if(i!=11&&i!=12){
	    	tt = tt+i;
	    	ab.setText(tt+"");
	    	//光标随字体移动
	    	//editText.requestFocus();
	    	ab.setSelection(ab.getText().length());
    	}else{
    		if(i==11){//拨打号码
    			String phime = ab.getText().toString();
    			 PhoneUtil.phoneNumber(PhoneActivity.this, phime.trim());
 				finish();
    		}else{
    			//退格
    			int pmm = ab.getText().toString().length();
    			String phh = ab.getText().toString();
    			if(pmm>0){
    				tt=phh.substring(0, pmm-1);
    				ab.setText(tt);
    				ab.setSelection(ab.getText().length());//焦点需要改变
    			}else{
    				Toast.makeText(this, "没有了", 0).show();
    			}
    		}
    	}
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	reshowButton();
    }
    /**
     * 弹出收回开关
     */
    private void toggleButton() {
    	if (mAreButtonsShowins) {
    		AnimationControlUtils.startAnimations(mButtonsWrapper, Direction.OUT);
    		// 红色按钮内白色十字旋转
    		mButtonControlIcon.startAnimation(mRotateOutAniamtion);
		} else {
			AnimationControlUtils.startAnimations(mButtonsWrapper, Direction.IN);
			mButtonControlIcon.startAnimation(mRotateInAnimation);
		}    	
    	mAreButtonsShowins = !mAreButtonsShowins;
    }
    
    
    /**
     * 弹出收回按钮点击后动画
     * 点击后变大后消失
     * @param view
     */
//    private void startButtonClickAnimations(View subject) {
//    	mAreButtonsShowins = false;    	
//    	// 红色按钮动画
//    	AnimationSet shrinkAnimationSet = new NotClickAnimationSet(600);
//    	shrinkAnimationSet.setInterpolator(new AnticipateInterpolator(2.0F));
////    可以加参数，本项目不需要消失
//    	shrinkAnimationSet.setAnimationListener(new CustomAnimationListener());
//    	mControlButton.startAnimation(shrinkAnimationSet);    	
//    	
//    	// 为每一个按钮都设置动画
//    	for (int i = 0, count = mButtonsWrapper.getChildCount(); i < count; i++) {
//			if (mButtonsWrapper.getChildAt(i) instanceof InOutImageButton) {
//				View view = mButtonsWrapper.getChildAt(i);
//				if (view.getId() == subject.getId()) {
//					// 点击按钮放大并消失
//					view.startAnimation(new ClickAnimationSet(600));
//				} else {
//					// 未点击按钮缩小并消失
//					view.startAnimation(new NotClickAnimationSet(600));
//				}
//			}
//		}
//    }   
	
	/**
	 * 跳转回来重置
	 */
	private void reshowButton() {		
		// 尺寸，透明度渐变
		ResetAnimationSet resetAnimationSet = new ResetAnimationSet(300);
		resetAnimationSet.setInterpolator(new OvershootInterpolator(2.0F));
		mControlButton.startAnimation(resetAnimationSet);
		
//		mButtonControlIcon.startAnimation(mRotateOutAniamtion);
	}
	
    /**
     *	按钮点击监听器
     */
//    private class CustomAnimationListener implements AnimationListener {
//
//		@Override
//		public void onAnimationStart(Animation animation) {
//		}
//
//		@Override
//		public void onAnimationEnd(Animation animation) {
//			
////			Toast.makeText(getApplicationContext(), "1"+animation.getClass(), 1000).show();
////			Intent intent = new Intent(MainActivity.this, MockActivity.class);
////			MainActivity.this.startActivity(intent);
//		}
//
//		@Override
//		public void onAnimationRepeat(Animation animation) {
//			
//		}    	
//    }
 }